Repository navigation
feat(server): add read-only MCP usage tools - #749
Open
tannerpolley wants to merge 1 commit into
Open
tannerpolley wants to merge 1 commit into
tannerpolley wants to merge 1 commit into
Conversation
Outside agents could read threads but not token, cost, or limit analytics. Add three read-only tools that use the saved usage scan, a bounded indexed turn sample, and the in-memory limits snapshot. Model: Grok 4.7 Harness: Cursor Co-authored-by: Tanner Polley <tannerwpolley@gmail.com>
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
Outside agents connected through T3 Connect can read and steer threads, but the official MCP server has no way to read token, cost, or subscription-limit analytics. A bot that should suggest how to use tokens better has nothing to call.
Change
Add three read-only tools on the environment MCP server. Read only access is enough. They do not return prompts, messages, secrets, account emails, credential ids, or file paths.
t3_usage_summaryreads the scan Usage already saved. It does not walk transcripts or resolve service tiers in the turn table. If that scan does not cover the requested 1–90 day window, the result isnot_cachedand no scan starts.t3_usage_threadsranks recent threads and projects and lists threads near automatic compaction. The query is capped at the 40 most recently updated threads, 4 provider threads each, and the latest 8 turns, using the existing usage and ordinal indexes on a read-only connection.t3_usage_limitsreturns the limits snapshot already in memory. It does not probe providers or redeem credits.UsageService.readCachedSummaryis the saved-scan path. A window the last complete scan did not cover returns without reading history.Example
t3_usage_summaryresultShape from the tool's fixture (not a live account). Reasoning is included in output. Cost is an API-equivalent estimate, not the subscription bill.
{ "status": "ready", "window": { "sinceDay": "2026-11-22", "untilDay": "2026-11-28", "timeZone": "UTC", "days": 7 }, "readAt": "2026-11-28T12:00:00.000Z", "refreshing": false, "pricing": { "status": "cached", "knownModels": 10 }, "totals": { "tokens": { "uncachedInput": 1200, "cachedInput": 4000, "cacheWrite": 1000, "output": 600, "reasoning": 100, "total": 6800 }, "cacheHitRate": 0.6452, "costUsd": 1.75, "cacheSavingsUsd": 0.4, "costPerMillionTokens": 257.35, "categoryCostUsd": { "input": 0.2, "cacheRead": 0.1, "cacheWrite": 0.05, "output": 0.15, "other": 1.25 }, "speedUsd": { "standard": 1.65, "fast": 0.1, "ultrafast": 0, "premium": 0.05 }, "records": 3, "sessions": 3, "unpricedTokens": 0, "costSourceShare": { "modelPriced": 0.6667, "providerReported": 0.3333, "unpriced": 0 } }, "byProvider": [{ "provider": "claude" }, { "provider": "codex" }], "byModel": [{ "provider": "claude", "model": "claude-opus" }, { "provider": "codex", "model": "gpt-test" }], "byDay": [ { "day": "2026-11-27", "tokens": 300, "costUsd": 0.25 }, { "day": "2026-11-28", "tokens": 6500, "costUsd": 1.5 } ] }t3_usage_threadsaddsthreads,projects, andnearCompaction. Turninputincludes cache reads and writes. ContextnearAutoCompactionis at least 80% of the automatic-compaction point, or 85% of the max window when no compaction point is reported.t3_usage_limitsreturnsaccounts[]withwindows(usedPercent,remainingPercent,resetsAt) andresetCredits.availableCount, without emails or credit ids.Access and performance
MCP clients are approved for an access level, not a raw scope. These tools use the same read gate as listing projects and threads, so a Read only connection can call them and a write is impossible. The internal Usage RPC remains
diagnostics:read; that scope is not granted to MCP clients.The summary never starts the scan that has stalled long reads of the turn table. Thread rankings stay on indexed point reads (at most 40 × 4 × 8 rows) with a 2 second stop. Limits are the snapshot already held in memory.
Advertising the tools grows
mcpToolListJsonBytesfrom 63967 to 65960. CI will reject that until a maintainer adds theagent-context-increaselabel.Scope
Requested for the Token Optimizer bot on this fork: a fresh read-only usage tool, not a revival of any earlier budget report.
Verification
vp test run apps/server/src/mcp/toolkits/usage/handlers.test.ts— 8 passed. Covers totals, cache hit rate, cost per million, secret stripping, read-only access,not_cached, the saved-scan timeout, calendar windows, and the bounded thread query (latest 8 turns, local commands skipped, deleted and older threads left out, prompt text and workspace paths absent).vp test run apps/server/src/usage/UsageService.test.ts -t "reads a covered window from the saved scan"— passed. A later transcript line is invisible to the cached read, and a wider uncovered window returns undefined.effect-tsgo tsrs --noEmitonapps/serverreports no errors in these files. Lint is clean on the new toolkit.Rebuild or restart the server so
/mcpserves the new tools, then reconnect the outside agent (or refresh its tool list). An existing Read only grant does not need a new approval. No database migration. If Usage has not saved a scan for the requested range,t3_usage_summaryreturnsnot_cacheduntil that range is opened in Usage.Model: Grok 4.7
Harness: Cursor